var express = require("express");
const Fly = require("flyio/src/node");
const fly = new Fly();
//引入db.js
require("./tools/db");
//引入student.js
var Student = require("./tools/student");

var bodyParser = require("body-parser");
var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//接口
// app.get("/test", function (req, res) {
//   //增
//   Student.create(
//     {
//       name: "二郎神",
//       age: "48",
//       gender: "男",
//       address: "小圣庙",
//     },
//     function (err) {
//       if (!err) {
//         console.log("插入成功");
//       }
//     }
//   );
// });

// 登录
app.post("/login", async (req, res) => {
  //   console.log("req", req.body);
  const { phone } = req.body;
  const result = await Student.find({ phone });
  if (!result.length) {
    res.send("输入的手机号不正确");
    return;
  } else {
    console.log(result);
    res.send(result);
  }
});

// 注册
const myOrder = require("./datas/myOrder.json");
app.post("/register", async (req, res) => {
  //   console.log("req", req.body);
  const { phone, password } = req.body;
  Student.create(
    {
      phone,
      password,
      titleImg:
        "https://s0.meituan.net/bs/fe-web-meituan/e3064a3/img/head-img.png",
      myOrder,
    },
    function (err) {
      if (!err) {
        console.log("注册成功");
      }
    }
  );
  //   console.log(result);
  res.send("注册成功");
});

// 我的订单
const userInfo = require("./datas/userInfo.json");
app.get("/userInfo", (req, res) => {
  res.send(userInfo);
});

// 我的订单
app.get("/myOrder", (req, res) => {
  res.send(myOrder);
});

// 猜你喜欢
const youLike = require("./datas/youLike.json");
app.get("/youLike", async (req, res) => {
  res.send(youLike);
});

// 猫眼电影
app.get("/film", async (req, res) => {
  const url = "https://gz.meituan.com/ptapi/getHotFilms?ci=20&limit=10";
  const { data } = await fly.get(url);
  res.send(data);
});
// 搜索

app.get("/search", async (req, res) => {
  const { keyword } = req.query;
  // console.log(encodeURI(keyword));
  const url =
    "https://www.meituan.com/ptapi/suggest?keyword=" + encodeURI(keyword);
  // console.log(url);
  const { data } = await fly.get(url);
  res.send(data);
});

// 即将上映
app.get("/comingSoon", async (req, res) => {
  const url = "https://gz.meituan.com/ptapi/getComingFilms?ci=20&limit=10";
  const { data } = await fly.get(url);
  res.send(data);
});

// 获取推荐民宿城市列表
app.get("/getCityList", async (req, res) => {
  const url = "https://gz.meituan.com/ptapi/minsuCitys?fetchSize=10";
  const { data } = await fly.get(url);
  res.send(data);
});

// 获取附近商家
const aroundShop = require("./datas/aroundShop.json");
app.get("/around", async (req, res) => {
  res.send(aroundShop);
});

// 获取卡片的图片列表
const cardList = require("./datas/cardList.json");
app.get("/cardList", async (req, res) => {
  res.send(cardList);
});

// 根据城市id获取民宿详细
app.get("/getHotelList", async (req, res) => {
  const { id } = req.query;
  const url = "https://gz.meituan.com/ptapi/minsu?cityId=" + id;
  //   console.log(url);
  const { data } = await fly.get(url);
  res.send(data);
});

// 获取商品列表
const goodsList = require("./datas/goodsList.json");
app.get("/goodsList", async (req, res) => {
  let { page } = req.query;
  page = (page - 1) * 15;
  const goods = goodsList.slice(page, page + 15);
  res.send(goods);
});

// 获取评论列表
const comment = require("./datas/comment.json");
app.get("/comment", (req, res) => {
  let { page } = req.query;
  page = (page - 1) * 10;
  const list = comment.comments.slice(page, page + 10);
  const total = comment.total;
  res.send({ list, total });
});

// 通过美团服务器返回评论
// https://i.meituan.com/xiuxianyule/api/getCommentList?poiId=96256935&offset=0&pageSize=10
app.get("/getCommentList", async (req, res) => {
  const { poiId, offset, pageSize } = req.query;
  const url = `https://i.meituan.com/xiuxianyule/api/getCommentList?poiId=${poiId}&offset=${offset}&pageSize=${pageSize}`;
  //   console.log(url);
  const { data } = await fly.get(url);
  res.send(data);
});

// 通过美团服务器返回附近商家
// https://i.meituan.com/xiuxianyule/api/getNearPoiList?poiId=96256935&start=0&limit=10
app.get("/getNearPoiList", async (req, res) => {
  const { poiId, start, limit } = req.query;
  const url = `https://i.meituan.com/xiuxianyule/api/getNearPoiList?poiId=${poiId}&start=${start}&limit=${limit}`;
  //   console.log(url);
  const { data } = await fly.get(url);
  res.send(data);
});

// 通过美团服务器返回猜你喜欢
// https://i.meituan.com/xiuxianyule/api/getRecommendList?poiId=96256935
app.get("/getRecommendList", async (req, res) => {
  const { poiId } = req.query;
  const url = `https://i.meituan.com/xiuxianyule/api/getRecommendList?poiId=${poiId}`;
  //   console.log(url);
  const { data } = await fly.get(url);
  res.send(data);
});

// 获取评论标签列表
const tags = require("./datas/tags.json");
app.get("/tags", async (req, res) => {
  res.send(tags);
});

// 获取城市列表
const cityList = require("./datas/cityList.json");
app.get("/cityList", async (req, res) => {
  res.send(cityList);
});

// 获取菜品
const foodList = require("./datas/foodList.json");
app.get("/foodList", async (req, res) => {
  res.send(foodList);
});

// 休闲娱乐导航
const recreationList = require("./datas/recreationList.json");
app.get("/recreationList", async (req, res) => {
  res.send(recreationList);
});
// 休闲娱乐菜单
const recreationInfo = require("./datas/recreationInfo.json");
app.get("/recreationInfo", async (req, res) => {
  let { page } = req.query;
  page = (page - 1) * 25;
  const goods = recreationInfo.slice(page, page + 25);
  res.send(goods);
});

// 解决跨域
app.all("*", function (req, res, next) {
  console.log(req.method);
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Content-type");
  res.header(
    "Access-Control-Allow-Methods",
    "PUT,POST,GET,DELETE,OPTIONS,PATCH"
  );
  res.header("Access-Control-Max-Age", 1728000); //预请求缓存20天
  next();
});

// 开启服务器
app.listen("5000", (err) => {
  if (err) console.log(err + "服务器启动失败");
  else console.log("服务器启动成功:http://localhost:5000");
});
